package com.cssw.fyzb.busi.api.turntable;

import com.cssw.fyzb.busi.api.user.GetUserInfo;
import com.cssw.fyzb.busi.service.turntable.TurntableService;
import com.cssw.fyzb.core.model.dodb.Acct;
import com.cssw.fyzb.core.model.dodb.TurntableNumPrice;
import com.cssw.fyzb.core.model.dodb.TurntableNumPriceLog;
import com.cssw.fyzb.core.model.vo.acct.AcctGlobalVo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.core.model.vo.turntable.GetInfoVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;


/**
 * 获取用户抽奖信息
 *
 * asa
 */
@Function(description = "获取用户抽奖信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})

@Component
public class GetTurnableInfo implements Action {

    @Autowired
    private AcctService acctService;

    @Autowired
    private TurntableService turntableService;

    private static final Logger logger = LoggerFactory.getLogger(GetTurnableInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");

        try {
            Acct acct = acctService.queryAcctByUIdAndType(userId, "F");
            GetInfoVo getInfoVo = new GetInfoVo();
            getInfoVo.setOneMoreTimes(0);
            BigDecimal balanceFee = acct.getBalanceFee();
            getInfoVo.setFBalance(balanceFee);
            if(balanceFee.compareTo(new BigDecimal(10))<0){
                getInfoVo.setCanTurn(false);
            }else{
                getInfoVo.setCanTurn(true);
            }
            TurntableNumPriceLog numPriceLog = turntableService.getTurnNumPriceLog(userId);
            if (null != numPriceLog){
                getInfoVo.setAddedPrice(numPriceLog.getPrice().setScale(4, BigDecimal.ROUND_DOWN));
                getInfoVo.setTurnNums(numPriceLog.getNum());
            }else {
                getInfoVo.setAddedPrice(BigDecimal.ZERO);
                getInfoVo.setTurnNums(0);
            }
            return Response.success().put("data", getInfoVo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }
}



